<?php
namespace Admin\Controller;
use Think\Controller;
class GroupController extends BaseController {
    public function index(){
        $did = session('did');
        $sid = session('sid');
        $gid = session('gid');
        $tid = session('tid');
        
        $where1['roleid'] = 1;
        $where2['roleid'] = 2;
        $where3['roleid'] = 3;
        $where4['roleid'] = 4;

        $userModel = D('user');
        $departmentModel = D('department');
        $department = $departmentModel->select();
        if(session('roleid')==0){
            //取出部门
            $where['roleid'] = array(array('neq','5'),array('neq','0'));
        }elseif(session('roleid')==1){
            $where['did'] = $did;
            // $where['roleid'] = array('exp','in(2,3,4)');
        }elseif(session('roleid')==2){
            $where['did'] = $did;
            $where['sid'] = $sid;
            // $where['roleid'] = array('exp','in(3,4)');
        }elseif(session('roleid')==3){
            $where['did'] = $did;
            $where['sid'] = $sid;
            $where['gid'] = $gid;
            // $where['roleid'] = array('exp','in(4)');
        }elseif(session('roleid')==4){
            $where['did'] = $did;
            $where['sid'] = $sid;
            $where['gid'] = $gid;
            $where['tid'] = $tid;
        }
        $zjl = $userModel->where($where)->where('roleid=1')->select();
        $zj = $userModel->where($where)->where('roleid=2')->select();
        $jl = $userModel->where($where)->where('roleid=3')->select();
        $zr = $userModel->where($where)->where('roleid=4')->select();
        $yg = $userModel->where($where)->where('roleid=5')->select();
        // echo $userModel->getLastSql();
        if(session('roleid')==0){
            $this->assign(array(
              'bumen' =>$department,
              'zjl' => $zjl,
              'zj' => $zj,
              'jl' => $jl,
              'zr' => $zr,
            ));
        }elseif(session('roleid')==1){
          $this->assign(array(
              'bumen' => $department,
              'zj' => $zj,
              'jl' => $jl,
              'zr' => $zr,
            ));
        }elseif(session('roleid')==2){
          $this->assign(array(
              'bumen' => $department,
              'jl' => $jl,
              'zr' => $zr,
            ));
        }elseif(session('roleid')==3){
          $this->assign(array(
              'bumen' => $department,
              'zr' => $zr,
            ));
        }elseif(session('roleid')==4){
          $this->assign(array(
              'user'=>$yg,
            ));
        }
        
        //根据不同的roleid载入页面
        if(session('roleid')==0){
           $this->display('admin'); 
        }elseif(session('roleid')==1){
            $this->display('zongjingli');
        }elseif(session('roleid')==2){
            $this->display('zongjian');
        }elseif(session('roleid')==3){
            $this->display('jingli');
        }elseif(session('roleid')==4){
            $this->display('zhuren');
        }
    }
    //新增部门
    public function addBuMen(){
      $name = I('post.departmentname');
      $departmentModel = D('department');
      $res = $departmentModel->add(array('dname' => $name, ));
      if($res){
        echo json_encode(array('ok' =>1 , ));
      }else{
        echo json_encode(array('ok' =>2 , ));
      }
    }
    //删除部门
    public function delBumen(){
        $did = I('post.did');
        //查看该部门是否有成员
        $userModel = D('user');
        $user = $userModel->where(array('did' =>$did , ))->count();
        if($user){
            echo json_encode(array('ok' =>0 , ));
            exit;
        }
        
        //删除分组
        $groupModel = D('group');
        $groupInfo = $groupModel->where(array('did' => $did, ))->select();
        if($groupInfo){
            $userModel->startTrans();
            foreach ($groupInfo as $k => $v) {
                $delGroup = $groupModel->delete($v['gid']);
            }

            //开启事务
            //删除部门
           $departmentModel = D('department');
           $res = $departmentModel->delete($did);
            //成功提交
           if($res && $delGroup){
              $userModel->commit();
              echo json_encode(array('ok' => 1, ));
           }else{
              $userModel->rollback();
              echo json_encode(array('ok' =>2 , ));
           }
           exit;
        }
        //删除部门
        $departmentModel = D('department');
        $res = $departmentModel->delete($did);
        if($res){
           echo json_encode(array('ok' => 1, )); 
        }else{
           echo json_encode(array('ok' =>2 , ));
      }
        
    }
    //删除总经理
    public function delJingLi(){
         $userId = I('post.userid');
         $updatetime = time();
         //删除用户
         $userModel = D('user');
         $count = $userModel->find($userId);
         if(!$count){
            echo json_encode(array('ok' => 0, ));
            exit;
         }
         
         //资源返回管理员
         $resourceModel = D('resource');
         $upres = $resourceModel->field('id')->where(array('userid' =>$userId , ))->select();
         if($upres){
            //开启事务
            $userModel->startTrans();
           //循环修改数据
             $count1 = count($resCount);
             $cou = floor($count1/100000);
             $yushu = $count1-($cou*100000);
             if($yushu){
                 $num=$cou+1;
             }else{
                 $num=$cou;
             }
             for ($i=1; $i <= $num; $i++) { 
                if($i==$num && $yushu!=""){
                    $count1= $yushu;
                }else{
                     $count1=100000;
                }
               $start = ($i-1)*100000;
               $shuju = array_slice($resCount,$start,$count1);
                $arr = array();
                foreach ($shuju as $k=>$r) {
                   foreach ($r as $k => $v) {
                     $arr[] = $v;
                   }    
                }         
              $exp = implode(',',$arr);
              $sql = "UPDATE `cms_resource` SET `updatetime`='".$updatetime."',`userid`='1' WHERE `id` in (".$exp.");";
              $upStatic = $resourceModel->query($sql);
           }
            $res = $userModel->delete($userId);
            //如果都成功就提交
            if($res && FALSE!==$upStatic){
               $userModel->commit();
               echo json_encode(array('ok' => 1, ));
            }else{ 
               //失败返回
               $userModel->rollback();
               echo json_encode(array('ok' =>2 , ));
            }
          exit;
         }
         //没有资源执行
          $res = $userModel->delete($userId);
         if($res){
             echo json_encode(array('ok' => 1, )); 
          }else{
             echo json_encode(array('ok' =>2 , ));
          }
         
    }

    //删除总监
    public function delZongJian(){
         $userId = I('post.userid');
         $updatetime = time();
         //删除用户
         $userModel = D('user');
         $count = $userModel->find($userId);
         if(!$count){
            echo json_encode(array('ok' => 0, ));
            exit;
         }
         
         //资源返回管理员
         $resourceModel = D('resource');
         $upres = $resourceModel->field('id')->where(array('userid' =>$userId , ))->select();
         if($upres){
            //开启事务
            $userModel->startTrans();
           //循环修改数据
             $count1 = count($resCount);
             $cou = floor($count1/100000);
             $yushu = $count1-($cou*100000);
             if($yushu){
                 $num=$cou+1;
             }else{
                 $num=$cou;
             }
             for ($i=1; $i <= $num; $i++) { 
                if($i==$num && $yushu!=""){
                    $count1= $yushu;
                }else{
                     $count1=100000;
                }
               $start = ($i-1)*100000;
               $shuju = array_slice($resCount,$start,$count1);
                $arr = array();
                foreach ($shuju as $k=>$r) {
                   foreach ($r as $k => $v) {
                     $arr[] = $v;
                   }    
                }         
              $exp = implode(',',$arr);
              $sql = "UPDATE `cms_resource` SET `updatetime`='".$updatetime."',`userid`='2' WHERE `id` in (".$exp.");";
              $upStatic = $resourceModel->query($sql);
           }
            $res = $userModel->delete($userId);
            //如果都成功就提交
            if($res && FALSE!==$upStatic){
               $userModel->commit();
               echo json_encode(array('ok' => 1, ));
            }else{ 
               //失败返回
               $userModel->rollback();
               echo json_encode(array('ok' =>2 , ));
            }
          exit;
         }
         //没有资源执行
          $res = $userModel->delete($userId);
         if($res){
             echo json_encode(array('ok' => 1, )); 
          }else{
             echo json_encode(array('ok' =>2 , ));
          }
         
    }

    //删除经理
    public function delJingLi2(){
         $userId = I('post.userid');
         $updatetime = time();
         //删除用户
         $userModel = D('user');
         $count = $userModel->find($userId);
         if(!$count){
            echo json_encode(array('ok' => 0, ));
            exit;
         }
         
         //资源返回管理员
         $resourceModel = D('resource');
         $upres = $resourceModel->field('id')->where(array('userid' =>$userId , ))->select();
         if($upres){
            //开启事务
            $userModel->startTrans();
           //循环修改数据
             $count1 = count($resCount);
             $cou = floor($count1/100000);
             $yushu = $count1-($cou*100000);
             if($yushu){
                 $num=$cou+1;
             }else{
                 $num=$cou;
             }
             for ($i=1; $i <= $num; $i++) { 
                if($i==$num && $yushu!=""){
                    $count1= $yushu;
                }else{
                     $count1=100000;
                }
               $start = ($i-1)*100000;
               $shuju = array_slice($resCount,$start,$count1);
                $arr = array();
                foreach ($shuju as $k=>$r) {
                   foreach ($r as $k => $v) {
                     $arr[] = $v;
                   }    
                }         
              $exp = implode(',',$arr);
              $sql = "UPDATE `cms_resource` SET `updatetime`='".$updatetime."',`userid`='3' WHERE `id` in (".$exp.");";
              $upStatic = $resourceModel->query($sql);
           }
            $res = $userModel->delete($userId);
            //如果都成功就提交
            if($res && FALSE!==$upStatic){
               $userModel->commit();
               echo json_encode(array('ok' => 1, ));
            }else{ 
               //失败返回
               $userModel->rollback();
               echo json_encode(array('ok' =>2 , ));
            }
          exit;
         }
         //没有资源执行
          $res = $userModel->delete($userId);
         if($res){
             echo json_encode(array('ok' => 1, )); 
          }else{
             echo json_encode(array('ok' =>2 , ));
          }
         
    }

    //删除主任
    // public function delZhuRen(){
    //      $userId = I('post.userid');
    //      $updatetime = time();
    //      //删除用户
    //      $userModel = D('user');
    //      $count = $userModel->find($userId);
    //      if(!$count){
    //         echo json_encode(array('ok' => 0, ));
    //         exit;
    //      }
         
    //      //资源返回管理员
    //      $resourceModel = D('resource');
    //      $upres = $resourceModel->field('id')->where(array('userid' =>$userId , ))->select();
    //      if($upres){
    //         //开启事务
    //         $userModel->startTrans();
    //        //循环修改数据
    //          $count1 = count($resCount);
    //          $cou = floor($count1/100000);
    //          $yushu = $count1-($cou*100000);
    //          if($yushu){
    //              $num=$cou+1;
    //          }else{
    //              $num=$cou;
    //          }
    //          for ($i=1; $i <= $num; $i++) { 
    //             if($i==$num && $yushu!=""){
    //                 $count1= $yushu;
    //             }else{
    //                  $count1=100000;
    //             }
    //            $start = ($i-1)*100000;
    //            $shuju = array_slice($resCount,$start,$count1);
    //             $arr = array();
    //             foreach ($shuju as $k=>$r) {
    //                foreach ($r as $k => $v) {
    //                  $arr[] = $v;
    //                }    
    //             }         
    //           $exp = implode(',',$arr);
    //           $sql = "UPDATE `cms_resource` SET `updatetime`='".$updatetime."',`userid`='4' WHERE `id` in (".$exp.");";
    //           $upStatic = $resourceModel->query($sql);
    //        }
    //         $res = $userModel->delete($userId);
    //         //如果都成功就提交
    //         if($res && FALSE!==$upStatic){
    //            $userModel->commit();
    //            echo json_encode(array('ok' => 1, ));
    //         }else{ 
    //            //失败返回
    //            $userModel->rollback();
    //            echo json_encode(array('ok' =>2 , ));
    //         }
    //       exit;
    //      }
    //      //没有资源执行
    //       $res = $userModel->delete($userId);
    //      if($res){
    //          echo json_encode(array('ok' => 1, )); 
    //       }else{
    //          echo json_encode(array('ok' =>2 , ));
    //       }
         
    // }



    //新增经理
    public function addJingLi(){
        $loginName =I('post.loginName');
        $did = I('post.did');
        $data['gid'] = 0;
        $data['roleid'] = 1;
        $data['sid'] = 0;
        $data['tid'] = 0;
        $userModel = D('user');
        //查看是否有该员工
        $res = $userModel->where(array('did' =>$did ,'loginname' =>$loginName, ))->find();
        if($res){
            if($res['roleid']==1){
              echo json_encode(array('ok' =>0 , ));
              exit;
            }
            $info = $userModel->where(array('did' => $did,'loginname'=>$loginName ))->save($data);
            if(FALSE!==$info){
              echo json_encode(array('ok' =>1 , ));
            }else{
              echo json_encode(array('ok' =>3 , ));
            }
        }else{
            echo json_encode(array('ok' =>2 , ));
        }
    }

    //经理
    //新增分组
    public function addGroup(){
        $data['did'] = session('did');
        $data['gname'] = I('post.name');
        $groupModel = D('group');
        $res = $groupModel->add($data);
        if($res){
            echo json_encode(array('ok' =>1, ));
        }else{
            echo json_encode(array('ok' =>2 , ));
        }
    }
    //删除分组
    public function delGroup(){
        $gid = I('post.gid');
        //查看组下是否有成员
        $userModel = D('user');
        $user = $userModel->where(array('gid' =>$gid , ))->count();
        if($user){
            echo json_encode(array('ok' =>0 , ));
            exit;
        }
        //否则删除
        $groupModel = D('group');
        $res = $groupModel->delete($gid);
        if($res){
            echo json_encode(array('ok' =>1 , ));
        }else{
            echo json_encode(array('ok' =>2 , ));
        }
    }
    //新增主任
    public function addZhuRen(){
        $zhuren = I('post.zhuren');
        if(empty(I('post.quid'))){
          $quid = session('did');
        }else{
          $quid = I('post.quid');
        }

        if(empty(I('post.bumenid'))){
          $bumenid = session('sid');
        }else{
          $bumenid = I('post.bumenid');
        }

        if(empty(I('post.zuid'))){
          $zuid = session('gid');
        }else{
          $zuid = I('post.zuid');
        }

        if(empty(I('post.duiid'))){
          $duiid = session('tid');
        }else{
          $duiid = I('post.duiid');
        }
        
        $data['roleid']=4;
        $userModel = D('user');
        //查看是否有该员工
        $res = $userModel->where(array('did' =>$quid,'sid'=>$bumenid,'gid'=>$zuid,'tid'=>$duiid,'loginname'=>$zhuren))->find();

        if($res){
            if($res['roleid']==4){
               echo json_encode(array('ok' =>0 , ));  //该成员已经是主任
                exit;
               }
               $result_update = $userModel->where(array('loginname'=>$zhuren))->save($data);
               if($result_update){
                echo json_encode(array('ok'=>1,)); //更新成功
               }else{
                echo json_encode(array('ok'=>2,)); //更新失败
               }
            }else{
          echo json_encode(array('ok'=>3,)); //没有该成员
     }
   }
    //删除主任
    public function delZhuRen(){
        $userid = I('post.userid');
        $onjob=1;
        $userModel = D('user');
        $count = $userModel->where(array('userId' =>$userid ,))->find();
        //没有此员工
        if(!$count){
            echo json_encode(array('ok' => 0, ));
            exit;
        }
            //把资源的离职状态改为离职
            $resourceModel = D('resource'); 
            $resCount = $resourceModel->field('id')->where(array('userid' =>$userid , ))->select();
            if($resCount){
               //开启实务
                $userModel->startTrans();
                //循环修改数据
                $count1 = count($resCount);
                $cou = floor($count1/100000);
                $yushu = $count1-($cou*100000);
                if($yushu){
                   $num=$cou+1;
                }else{
                   $num=$cou;
                }
                for ($i=1; $i <= $num; $i++) { 
                  if($i==$num && $yushu!=""){
                      $count1= $yushu;
                  }else{
                       $count1=100000;
                  }
                  $start = ($i-1)*100000;
                  $shuju = array_slice($resCount,$start,$count1);
                  $arr = array();
                  foreach ($shuju as $k=>$r) {
                     foreach ($r as $k => $v) {
                       $arr[] = $v;
                     }    
                  }         
                  $exp = implode(',',$arr);
                  $sql = "UPDATE `cms_resource` SET `updatetime`='".$updatetime."',`onjob`='".$onjob."' WHERE `id` in (".$exp.");";
                  $upStatic = $resourceModel->query($sql);
                }
                //有就删除员工
                $res = $userModel->delete($userid);
                //成功提交
                if($res && FALSE!==$upStatic){
                    $userModel->commit();
                    echo json_encode(array('ok' =>1 , ));
                }else{
                    //失败返回
                    echo json_encode(array('ok' =>2 , ));
                    $userModel->rollback();
                }
                exit;
            }
            //没有分配资源执行
             $res = $userModel->delete($userid);
             if($res){
                echo json_encode(array('ok' =>1 , ));
            }else{
                echo json_encode(array('ok' =>2 , ));
            }
            
    }
    
   
    //主任
    public function guanLiZuYuan(){
        $userid = I('get.userid');
        $sid = I('get.sid');
        $user = D('user')->field('username')->find($userid);
        $resourceModel = D('resource');
        $resourceInfo = $resourceModel->zuyuaninfo($userid,$sid);
    
        $this->assign(array(
            'userid'=>$userid,
            'sid'=>$sid,
            'username'=>$user['username'],
            'resource' =>$resourceInfo['data'] , 
            'page' =>$resourceInfo['page'],
            'count' =>$resourceInfo['count'],
            'resPage' =>$resourceInfo['resPage'],
            ));
        $this->display();
    }
    //删除员工
    public function delYuanGong(){
        $did = session('did');
        $sid = session('sid');
        $gid = session('gid');
        $tid = session('tid');
        $onjob=1;
        $updatetime=time();
        $loginname = I('post.loginname');
        $userModel = D('user');
        $count = $userModel->where(array('loginname' =>$loginname ,'did'=>$did,'gid'=>$gid,'sid'=>$sid,'tid'=>$tid ))->find();
        //没有此员工
        if(!$count){
            echo json_encode(array('ok' => 0, ));
            exit;
        }
            //把资源的离职状态改为离职
        $resourceModel = D('resource');
        $resCount = $resourceModel->field('id')->where(array('userid' =>$count['userid'] , ))->select();
         
        if($resCount){
          //开启实务
           $userModel->startTrans();
           //循环修改数据
             $count1 = count($resCount);
             $cou = floor($count1/100000);
             $yushu = $count1-($cou*100000);
             if($yushu){
                 $num=$cou+1;
             }else{
                 $num=$cou;
             }
             for ($i=1; $i <= $num; $i++) { 
                if($i==$num && $yushu!=""){
                    $count1= $yushu;
                }else{
                     $count1=100000;
                }
               $start = ($i-1)*100000;
               $shuju = array_slice($resCount,$start,$count1);
                $arr = array();
                foreach ($shuju as $k=>$r) {
                   foreach ($r as $k => $v) {
                     $arr[] = $v;
                   }    
                }         
              $exp = implode(',',$arr);
              $sql = "UPDATE `cms_resource` SET `updatetime`='".$updatetime."',`onjob`='".$onjob."' WHERE `id` in (".$exp.");";
              $upStatic = $resourceModel->query($sql);
          }
               //有就删除
            $res = $userModel->delete($count['userid']);
              //成功提交
            if($res && FALSE!==$upStatic){
                  $userModel->commit(); 
                  echo json_encode(array('ok' =>1 , ));   
            }else{
                  $userModel->rollback();
                  //失败返回
                  echo json_encode(array('ok' =>2 , )); 
            }
        }else{
            //没有资源执行
            $res = $userModel->delete($count['userid']);
            if($res){
               echo json_encode(array('ok' =>1 , )); 
           }else{
               echo json_encode(array('ok' =>2 , ));
           }
        }
    }
    //添加总监
    public function addzongjian()
    {

        if(empty(I('post.addzongjianqu'))){
          $quid = session('did');
        }else{
          $quid = I('post.addzongjianqu');
        }

        if(empty(I('post.addzongjianbu'))){
          $buid = session('sid');
        }else{
          $buid = I('post.addzongjianbu');
        }
        $zongjianname = I('post.addzongjianname');
        $data['roleid'] = 2;
        $data['gid'] = 0;
        $data['tid'] = 0;
        $user = d('user');
        //查看是否有该员工
        $res = $user->where(array('did'=>$quid,'gid'=>$buid,'loginname'=>$zongjianname))->find();
        if($res){
          if($res['roleid'] == 2){
            echo json_encode(array('ok'=>0,));  //0代表当前这个用户已经是总监
            die;
          }
          $result_update = $user->where("loginname= '%s'",$zongjianname)->save($data);
          
          if($result_update){
            echo json_encode(array('ok'=>1, )); //1代表更改成功
          }else{
            echo json_encode(array('ok'=>2,)); //2代表更新失败
          }
        }else{
          echo json_encode(array('ok'=>3,));  //3代表没有该员工
        }
    }

    //添加经理

    public function addJingLi2()
    {
      if(empty(I('post.quid'))){
        $quid = session('did');
      }else{
        $quid = I('post.quid');
      }
      if(empty(I('post.bumenid'))){
        $bumenid = session('sid');
      }else{
        $bumenid = I('post.bumenid');
      }
      if(empty(I('post.jinglizu'))){
        $zuid = session('sid');
      }else{
        $zuid = I('post.jinglizu');
      }
      $jingli = I('post.jingli');
      $data['roleid'] = 3; //经理的级别为3
      $data['tid'] = 0;
      //查看是否有该员工
      $userModel = D("user");
      $res = $userModel->where(array('did'=>$quid,'sid'=>$bumenid,'gid'=>$zuid,'loginname'=>$jingli))->select();
      if($res){
        if($res[0]['roleid'] == 3){
          echo json_encode(array('ok'=>0,));//该员工已经是经理
          die;
        }
        $result_update = $userModel->where(array('loginname'=>$jingli))->save($data);
        if($result_update){
          echo json_encode(array('ok'=>1,));//添加成功返回1
          die;
        }else{
          echo json_encode(array('ok'=>2,));//添加失败返回2
        }
      }else{
        echo json_encode(array('ok'=>3,));//没有该员工返回3
      }
    }

  //添加主任
    // public function addZhuRen2()
    // {
    //   $quid = I('post.quid');
    //   $bumenid = I('post.bumenid');
    //   $zuid = I('post.zuid');
    //   $duiid = I('post.duiid');
    //   $zhuren = I('post.zhuren');

    //   echo $quid,$bumenid,$zuid,$duiid,$zhuren;
    // }
}